#include "read.h"

int checkString(const char *str)
{
	for (int i = 0; i < strlen(str); i++){
        if(str[i] == '='){
        	return 1;
		}
	}
	return 0;
}

void read_student_data_from_file(STUDENT *head, FILE *fp)
{
    if (fp == NULL || head == NULL)
    {
		return;
	}
    char buf[100] = {'\0'};
    char temp[50] = {'\0'};
    char temp2[50] = {'\0'};

	char number[50] = {'\0'};
	char name[50] = {'\0'};
    char phone[50] = {'\0'};
	int Chinese;
    int Math;
    int English;
    STUDENT *node;
    fgets(buf, sizeof(buf), fp);
	while (fgets(buf, sizeof(buf), fp) != NULL)
    {
        // printf("1buf:%s", buf);
        if (checkString(buf))
        {
	        sscanf(buf, " %[a-zA-Z] = \"%[^\"]", temp, temp2);
            // printf("%s\n", temp );
            // printf("2buf:%s", buf);
            if (strcmp(temp, "number") == 0)
            {
                sscanf(buf, " %[a-zA-Z] = \"%[^\"]", temp, number);
                // printf("1%s", number);

                fgets(buf, sizeof(buf), fp);
                sscanf(buf, " %[a-zA-Z] = \"%[^\"]", temp, name);
                // printf("2%s", name);

                fgets(buf, sizeof(buf), fp);
                sscanf(buf, " %[a-zA-Z] = \"%[^\"]", temp, phone);
                // printf("3%s", phone);

                node = creat_node_by_basic_information(number, name, phone);
                insert_node(head, node);
            }
            if (strcmp(temp, "Chinese") == 0)
            {
                // printf("%s\n", temp);
                sscanf(buf, " %[a-zA-Z] = \"%d\"", temp, &Chinese);
                // printf("%d", Chinese);
                fgets(buf, sizeof(buf), fp);
                sscanf(buf, " %[a-zA-Z] = \"%d\"", temp, &Math);
                // printf("%d", Math);
                fgets(buf, sizeof(buf), fp);
                sscanf(buf, " %[a-zA-Z] = \"%d\"", temp, &English);
                // printf("%d", English);
                insert_stuNode_score(head, name, Chinese, Math, English);
            }
        }
 	}
}